<?php
class Acount_model extends DS_Model{
    private $model;
    function __construct(){
        parent::__construct();
        $this->load->helpers('string_helper');
        $this->model='acount';  
    } 

    //根据条件查询一条信息
    public function onceInfo($where,$select){
    	$res=$this->db->where($where)->select($select)->order_by('id desc')->limit(1)->get($this->model)->row_array();
    	return $res;
    }
    //查询总的条数
    public function countLog($where){
    	$count=$this->db->where($where)->from($this->model)->count_all_results();
    	return $count;
    } 
     //添加
    public function insertInfo($data){
        $res=$this->db->insert($this->model,$data);
        return $res;
    }  
    //修改  
    public function updateInfo($data,$where){
        $res=$this->db->update($this->model,$data,$where);
        return $res;
    }
    public function getInfo($where){
        $res=$this->db->where(array('id'=>$where))->select("*")->order_by('id desc')->limit(1)->get($this->model)->row_array();
        return $res;
    }
     //根据条件查询所有信息
    public function allInfoorder($where,$select,$order_by){
        $res=$this->db->where($where)->select($select)->order_by($order_by)->get($this->model)->result_array();
        return $res;
    }

    /*
     * 短信发送
     *  1：注册，
        2：找回密码，
        3：动态登陆
     */
    public  function  setSendSms($phone,$type){
        if(!in_array($type,array(1,2,3,4,5))){
            $info=array('code'=>'404','msg'=>'请求类型不正确');
            return $info;
        }
        if($type==1){
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count>0){
                $info=array('code'=>'404','msg'=>'该手机号已被注册');
                return $info;
            }
            $count1=$this->countLog(array('phone'=>$phone,'phone_valid'=>0));
            if($count1>0){//发送过验证码
                $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>0),'phone_code_time');
                if(now()-$res['phone_code_time']<60){
                    $info=array('code'=>'404','msg'=>'提交过于频繁,'.(60-(now()-$res['phone_code_time'])).'s之后在操作');
                    return $info;
                }
                //----------测试部分
                $verify='123456';
                $data = array(
                        'phone_code_time' => now(),
                        'phone_code' => $verify,
                    );
                $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>0));
                if($v){
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                    return $info;
                }
                //-------

                //发送验证码-----------------------------------------------------------------------------------------------------------------
//                $verify = rand(10000, 99999);
//                $text="欢迎注册挪亚家，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
//                $back=$this->getVerityCode($phone,$text);
//                if($back==1){
//                    $data = array(
//                        'phone_code_time' => now(),
//                        'phone_code' => $verify,
//                    );
//                    $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>0));
//                    if($v){
//                        $insert=array(
//                            'type'=>1,
//                            'code'=>$verify,
//                            'content'=>$text,
//                            'phone'=>$phone,
//                            'send_status'=>1,
//                            'timeline'=>now()
//                        );
//                        $this->db->insert('note',$insert);
//                        $info=array('code'=>'200','msg'=>'短信验证码发送成功');
//                        return $info;
//                    }else{
//                        $info=array('code'=>'404','msg'=>'短信验证码修改失败');
//                        return $info;
//                    }
//                }else{
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
//                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
//                    return $info;
//                }
            }else{//没有发送验证码
                //-------------------测试部分
                $verify = '123456';
                $data=array(
                    'phone'=>$phone,
                    'phone_code'=>$verify,
                    'phone_code_time'=>now(),
                    'phone_valid'=>0
                );
                $v=$this->insertInfo($data);
                if($v){
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                    return $info;
                }

                //-------------


//                $verify = rand(10000, 99999);
//                $text="欢迎注册挪亚家，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
//                $back=$this->getVerityCode($phone,$text);
//                if($back==1){
//                    $data=array(
//                        'phone'=>$phone,
//                        'phone_code'=>$verify,
//                        'phone_code_time'=>now(),
//                        'phone_valid'=>0
//                    );
//                    $v=$this->insertInfo($data);
//                    if($v){
//                        $insert=array(
//                            'type'=>1,
//                            'code'=>$verify,
//                            'content'=>$text,
//                            'phone'=>$phone,
//                            'send_status'=>1,
//                            'timeline'=>now()
//                        );
//                        $this->db->insert('note',$insert);
//                        $info=array('code'=>'200','msg'=>'短信验证码发送成功');
//                        return $info;
//                    }else{
//                        $info=array('code'=>'404','msg'=>'短信验证码修改失败');
//                        return $info;
//                    }
//                }else{
//                    $insert=array(
//                        'type'=>1,
//                        'code'=>$verify,
//                        'content'=>$text,
//                        'phone'=>$phone,
//                        'send_status'=>2,
//                        'timeline'=>now()
//                    );
//                    $this->db->insert('note',$insert);
//                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
//                    return $info;
//                }
            }
        }elseif($type==2){
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
            if($count==0){
                $info=array('code'=>'404','msg'=>'该手机号未注册');
                return $info;
            }
            $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1,'audit'=>1));
            if($count==0){
                $info=array('code'=>'404','msg'=>'该手机号已被禁用');
                return $info;
            }
            $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>1),'id,find_pwd_code_time');
            if(!empty($res['find_pwd_code_time']) && (now()-$res['find_pwd_code_time']<60)){
                $info=array('code'=>'404','msg'=>'提交过于频繁,'.(60-(now()-$res['phone_code_time'])).'s之后在操作');
                return $info;
            }
            //-----------------测试部分
            $verify='123456';
            $data = array(
                'find_pwd_code_time' => now(),
                'find_pwd_code' => $verify,
            );
            $v=$this->updateInfo($data,array('phone'=>$phone,'id'=>$res['id']));
            if($v){
                $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                return $info;
            }else{
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            }
            //---------------------
        }else{//动态登录,贷款申请，智能匹配
            $count=$this->countLog(array('phone'=>$phone));
            if($count==0){
                $verify="123456";
                $data=array(
                    'phone'=>$phone,
                    'phone_code'=>$verify,
                    'phone_code_time'=>now(),
                    'phone_valid'=>0
                );
                $v=$this->insertInfo($data);
                if($v){
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                    return $info;
                }
            }else{
                $res=$this->onceInfo(array('phone'=>$phone),'phone_code_time');
                if(now()-$res['phone_code_time']<60){
                    $info=array('code'=>'404','msg'=>'提交过于频繁,'.(60-(now()-$res['phone_code_time'])).'s之后在操作');
                    return $info;
                }
                //----------测试部分
                $verify='123456';
                $data = array(
                    'phone_code_time' => now(),
                    'phone_code' => $verify,
                );
                $v=$this->updateInfo($data,array('phone'=>$phone));
                if($v){
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                    return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                    return $info;
                }
                //-----------------
            }
        }
    }
   
        // 注册是获取短信验证码
        // .检测手机号码是否存在
        // .检测是否发送过验证码
        // .如果发送了，是否在时间范围内，如果不在则重新发送
    
    public function getVerify($phone){
        $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
        if($count>0){
            $info=array('code'=>'404','msg'=>'手机号已被注册');
            return $info;
        }
        $count1=$this->countLog(array('phone'=>$phone,'phone_valid'=>0));
        if($count1>0){//发送过验证码
            $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>0),'phone_code_time');
            if(now()-$res['phone_code_time']<60){
                $info=array('code'=>'404','msg'=>'提交过于频繁,'.(now()-$res['phone_code_time']).'s之后在操作');
                return $info;
            }
            //发送验证码-----------------------------------------------------------------------------------------------------------------
            $verify = rand(10000, 99999);
            $text="欢迎注册挪亚家，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data = array(
                    'phone_code_time' => now(),
                    'phone_code' => $verify,
                );
                $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>0));
                if($v){
                    $insert=array(
                        'type'=>1,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                    return $info;
                }
            }else{
                $insert=array(
                    'type'=>1,
                    'code'=>$verify,
                    'content'=>$text,
                    'phone'=>$phone,
                    'send_status'=>2,
                    'timeline'=>now()
                );
                $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            }
        }else{//没有发送验证码
            $verify = rand(10000, 99999);
            $text="欢迎注册挪亚家，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data=array(
                    'phone'=>$phone,
                    'phone_code'=>$verify,
                    'phone_code_time'=>now(),
                    'phone_valid'=>0
                );
                $v=$this->insertInfo($data);
                if($v){
                    $insert=array(
                        'type'=>1,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;
                }else{
                     $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                     return $info;
                }
            }else{
                $insert=array(
                    'type'=>1,
                    'code'=>$verify,
                    'content'=>$text,
                    'phone'=>$phone,
                    'send_status'=>2,
                    'timeline'=>now()
                );
                $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            } 
        }
    }
    //创建用户并绑定
    public function getCreateUserAndBindingVerify($phone){
        $count=$this->countLog(array('phone'=>$phone,'phone_valid'=>1));
        if($count>0){
            $info=array('code'=>'404','msg'=>'手机号已被注册');
            return $info;
        }
        $count1=$this->countLog(array('phone'=>$phone,'phone_valid'=>0));
        if($count1>0){//发送过验证码
            $res=$this->onceInfo(array('phone'=>$phone,'phone_valid'=>0),'phone_code_time');
            if(now()-$res['phone_code_time']<60){
                $info=array('code'=>'404','msg'=>'提交过于频繁,'.(now()-$res['phone_code_time']).'s之后在操作');
                return $info;
            }
            //发送验证码-----------------------------------------------------------------------------------------------------------------
            $verify = rand(10000, 99999);
            $text="您好，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data = array(
                    'phone_code_time' => now(),
                    'phone_code' => $verify,
                );
                $v=$this->updateInfo($data,array('phone'=>$phone,'phone_valid'=>0));
                if($v){
                    $insert=array(
                        'type'=>1,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                    return $info;
                }
            }else{
                $insert=array(
                    'type'=>1,
                    'code'=>$verify,
                    'content'=>$text,
                    'phone'=>$phone,
                    'send_status'=>2,
                    'timeline'=>now()
                );
                $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            }
        }else{//没有发送验证码
            $verify = rand(10000, 99999);
            $text="您好，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data=array(
                    'phone'=>$phone,
                    'phone_code'=>$verify,
                    'phone_code_time'=>now(),
                    'phone_valid'=>0
                );
                $v=$this->insertInfo($data);
                if($v){
                    $insert=array(
                        'type'=>1,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;

                }else{
                     $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                     return $info;
                }
            }else{
                $insert=array(
                    'type'=>1,
                    'code'=>$verify,
                    'content'=>$text,
                    'phone'=>$phone,
                    'send_status'=>2,
                    'timeline'=>now()
                );
                $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                     return $info;
            }  
        }
    }
    //获取找回密码是的手机验证码
    public function getForgerPwdVerify($id,$phone,$find_pwd_code,$find_pwd_code_time){
            if(!empty($find_pwd_code_time) && (now()-$find_pwd_code_time<60)){
                $info=array('code'=>'404','msg'=>'提交过于频繁,'.(now()-$find_pwd_code_time).'s之后在操作');
                return $info;
            }
            $verify = rand(10000, 99999);
            $text="您好，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data = array(
                    'find_pwd_code_time' => now(),
                    'find_pwd_code' => $verify,
                );
                $v=$this->updateInfo($data,array('phone'=>$phone,'id'=>$id));
                if($v){
                    $insert=array(
                        'type'=>2,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                    return $info;
                }
            }else{
                $insert=array(
                        'type'=>2,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>2,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            }
            // $verify = '123456';
            // $data=array(
            //     'find_pwd_code'=>$verify,
            //     'find_pwd_code_time'=>now()
            // );
            // $v=$this->updateInfo($data,array('phone'=>$phone,'id'=>$id));
            // if($v){
            //     $info=array('code'=>'200','msg'=>'短信验证码发送成功');
            //     return $info;
            // }else{
            //     $info=array('code'=>'404','msg'=>'短信验证码发送失败');
            //     return $info;
            // }
        
    }
    //验证原手机验证码
    public function getOldPhoneVerify($id,$phone,$phone_code,$phone_code_time){
        if(!empty($phone_code_time) && (now()-$phone_code_time<60)){
            $info=array('code'=>'404','msg'=>'提交过于频繁,'.(now()-$phone_code_time).'s之后在操作');
            return $info;
        }
            $verify = rand(10000, 99999);
            $text="您好，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
            $back=$this->getVerityCode($phone,$text);
            if($back==1){
                $data = array(
                    'phone_code_time' => now(),
                    'phone_code' => $verify,
                );
                $v=$this->updateInfo($data,array('phone'=>$phone,'id'=>$id));
                if($v){
                    $insert=array(
                        'type'=>3,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>1,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                    $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                       return $info;
                }else{
                    $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                    return $info;
                }
            }else{
                $insert=array(
                        'type'=>3,
                        'code'=>$verify,
                        'content'=>$text,
                        'phone'=>$phone,
                        'send_status'=>2,
                        'timeline'=>now()
                    );
                    $this->db->insert('note',$insert);
                $info=array('code'=>'404','msg'=>'短信验证码发送失败');
                return $info;
            }
    }
    //给新手机号发送验证码
    public function getNewPhonecodeVerify($userid,$phone){
        $res=$this->onceInfo(array('id'=>$userid),'change_phone_code_time');
        if(!empty($res['change_phone_code_time']) && (now()-$res['change_phone_code_time']<60)){
            $info=array('code'=>'404','msg'=>'提交过于频繁,'.(now()-$res['change_phone_code_time']).'s之后在操作');
            return $info;
        }
        $verify = rand(10000, 99999);
        $text="您好，您的验证码为".$verify."，请在10分钟内完成验证，为保障账号安全，请勿转发验证码给他人。";
        $back=$this->getVerityCode($phone,$text);
        if($back==1){
            $data = array(
                'change_phone'=>$phone,
                'change_phone_code_time' => now(),
                'change_phone_code' => $verify,
            );
            $v=$this->updateInfo($data,array('id'=>$userid));
            if($v){
                $insert=array(
                    'type'=>4,
                    'code'=>$verify,
                    'content'=>$text,
                    'phone'=>$phone,
                    'send_status'=>1,
                    'timeline'=>now()
                );
                    $this->db->insert('note',$insert);
                $info=array('code'=>'200','msg'=>'短信验证码发送成功');
                   return $info;
            }else{
                $info=array('code'=>'404','msg'=>'短信验证码修改失败');
                return $info;
            }
        }else{
            $insert=array(
                'type'=>1,
                'code'=>$verify,
                'content'=>$text,
                'phone'=>$phone,
                'send_status'=>2,
                'timeline'=>now()
            );
            $this->db->insert('note',$insert);
            $info=array('code'=>'404','msg'=>'短信验证码发送失败');
            return $info;
        }
    }

    public function getVerityCode($tel,$text){
        $this->load->library('ChuanglanSmsHelper/ChuanglanSmsApi');
        $clapi  = new ChuanglanSmsApi();
        $code = mt_rand(100000,999999);
        $result = $clapi->sendSMS($tel, $text);
        if(!is_null(json_decode($result))){
            
            $output=json_decode($result,true);
            if(isset($output['code'])  && $output['code']=='0'){
                return 1 ;
            }else{
                return $output['errorMsg'];
            }
        }else{
                return $result;
        }
    }
}